

function CreatePlayer ( )
    spawner = FindAvailableSpawner ( )
    if pSaucer.Active = 0 and spawner <> 0
        if pSaucer.Sprite = 0
            pSaucer.Sprite = CreateSprite ( 0 )
            // Load images into player sprite for animation
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 1 ] )
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 2 ] )
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 3 ] )
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 4 ] )
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 5 ] )
            AddSpriteAnimationFrame ( pSaucer.Sprite, g_iPlayerImage [ 6 ] )
        else
            SetSpriteActive  ( pSaucer.Sprite, 1 )
            SetSpriteVisible ( pSaucer.Sprite, 1 )
        endif
        pSaucer.Active = 1

        PlaySprite ( pSaucer.Sprite, 30 )
        // Setup physics
        SetSpriteShape              ( pSaucer.Sprite, 3   )
        SetSpritePhysicsOn          ( pSaucer.Sprite, 2   )
        SetSpriteGroup              ( pSaucer.Sprite, 2   )
        SetSpritePhysicsCanRotate   ( pSaucer.Sprite, 0   )
        SetSpritePhysicsRestitution ( pSaucer.Sprite, 0.2 )
        SetSpritePhysicsMass        ( pSaucer.Sprite, 1   )
        SetSpriteActive             ( pSaucer.Sprite, 0   )
        // Place saucer at spawner
        SetSpritePositionByOffset ( pSaucer.Sprite, GetSpriteXByOffset ( g_iSpawner [ spawner ] ), GetSpriteYByOffset ( g_iSpawner [ spawner ] ) )
        // Initialize player
        ResetPlayer ( )
    endif
    if pSaucer.Active = 0 and spawner = 0
        AddToSpawnQueue ( -1 )
    endif
endfunction

function ResetPlayer ( )
    pSaucer.MaxVel = 400
    pSaucer.State  =  Spawning
    pSaucer.aiType =  0
    pSaucer.StateTimerGoal = timer() + SpawnDelay
    pSaucer.StateTimer     = timer()
endfunction

function DestroyPlayer ( )
    SetSpriteActive     ( pSaucer.Sprite, 0 )
    SetSpriteVisible    ( pSaucer.Sprite, 0 )
    SetSpritePosition   ( pSaucer.Sprite, 0, 0 )
    pSaucer.Active = 0
endfunction

function UpdatePlayer ( )
    select ( pSaucer.State )

        case Spawning:
            time# = timer()
            if time# < pSaucer.StateTimerGoal
                // Total vertical movement = Sprite Height       Scaling straight percentage
                SetSpritePositionByOffset ( pSaucer.Sprite , GetSpriteXByOffset ( pSaucer.Sprite ), GetSpriteYByOffset ( pSaucer.Sprite ) - ( ( 30.0 / SpawnDelay ) * ( time# - pSaucer.StateTimer ) ) )
                //SetSpriteScaleByOffset ( pSaucer.Sprite, 1.0 - ( pSaucer.StateTimer - time# ) / 2.0, 1.0 - ( pSaucer.StateTimer - time# ) / 2.0 )
                pSaucer.StateTimer = time#
            else
                // Finished spawning
                pSaucer.State = Idle
                SetSpriteScaleByOffset ( pSaucer.Sprite, 1, 1 )
                SetSpriteActive ( pSaucer.Sprite, 1 )
            endif
        endcase

        case Dying:
            //
            DestroyPlayer ( )
            AddToSpawnQueue ( -1 )
        endcase

        case Idle:
            MovePlayer ( )
            CheckPlayerAgainstEnemy ( )
        endcase

        case Moving:
            time# = timer()
            if time# < pSaucer.StateTimerGoal
                MovePlayer ( )
                CheckPlayerAgainstEnemy ( )
            else
                SetSpriteAngle ( pSaucer.Sprite, 0 )
                pSaucer.State = Idle
            endif
        endcase

    endselect
endfunction

function MovePlayer ( )
    if pSaucer.aiType = 0 then HandleInput ( )
    CheckBoundary ( pSaucer.Sprite, 1024, -64, 768, -64)
    CapSpeed ( pSaucer.Sprite, pSaucer.MaxVel )
endfunction

function CheckPlayerAgainstEnemy ( )
    killed = 0
    for index = 1 to MaxEnemies
        if pSaucer.Active = 1 and eSaucer [ index ].Active = 1
            if GetPhysicsCollision ( pSaucer.Sprite, eSaucer [ index ].Sprite ) = 1
                if GetSpriteYByOffset ( pSaucer.Sprite ) < GetSpriteYByOffset ( eSaucer [ index ].Sprite ) - 5 and killed = 0
                    eSaucer [ index ].State = Dying
                    eSaucer [ index ].StateTimerGoal = 0.1
                    killed = 1
                endif
                if GetSpriteYByOffset ( pSaucer.Sprite ) > GetSpriteYByOffset ( eSaucer [ index ].Sprite ) + 5 and killed = 0
                    pSaucer.State = Dying
                    pSaucer.StateTimerGoal = 0.01
                    killed = 1
                endif
            endif
        endif
    next index
endfunction
